<?php
namespace app\admin\controller;
class Userext extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [
                ['and','b.level','>',0]
            ];
            if($d['keyword']!='') $map[] = ['and','a.username|b.username','like',"%{$d['keyword']}%"];
            if($d['status']!='') $map[] = ['and','a.status','=',"%{$d['status']}%"];
            $w  = where($map);
            $rs = $this->db->query("
                select
                    a.*,b.username as uname
                from
                    user_ext  a
                inner join
                    user b on a.user_id = b.id
                $w[0] order by a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                foreach ($rs[0] as $k => $v) {
                    $rs[0][$k]["status_name"] = c("type.status")[$v['status']];
                    $rs[0][$k]["create_time_name"] = com_time_format($v['create_time']);
                }
                suc($rs[0],$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $rs = $this->db->find('user_ext',$d["id"]);
            $status = $rs["status"];
            if($status == 0){
                $status = 1;
            }else{
                $status = 0;
            }
            $id = $this->db->update('user_ext',["id"=>$d["id"],"status"=>$status]);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','reload');
                return suc();
            }else{
                err('修改失败');
            }
        }
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('user_ext',$d['id']);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }
}
